#include "widget_setup.h"
#include "../AI_Tester/init.h"
#include <string>
#include <iostream>



setup_widget::setup_widget( QWidget* parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{	
	layout = new QVBoxLayout( this );


	QWidget* title_w = new QWidget( this );
	QHBoxLayout* title_w_l = new QHBoxLayout( title_w );

	QLabel* title_l = new QLabel( title_w );
	title_l->setTextFormat( Qt::RichText );
	title_l->setText( "<b><font size=8>Server Setup</font></b>" );
	title_w_l->addWidget( title_l );

	listen_b = new QPushButton( "LISTEN", title_w );
	listen_b->setFixedWidth( 150 );
	listen_b->setEnabled( false );
	title_w_l->addWidget( listen_b );
	connect( listen_b, SIGNAL( clicked() ), this, SLOT( start_listen() ) );

	title_w->setLayout( title_w_l );
	layout->addWidget( title_w );


	layout->addSpacing( 20 );


	QWidget* port_w = new QWidget( this );
	QHBoxLayout* port_w_l = new QHBoxLayout( port_w );

	QLabel* port_l = new QLabel( port_w );
	port_l->setTextFormat( Qt::RichText );
	port_l->setText( "<font size=5>Listening port:</font>" );
	port_w_l->addWidget( port_l );

	port_i = new QLineEdit( port_w );
	port_i->setFixedWidth( 250 );
	port_i->setText( "1234" );
	port_w_l->addWidget( port_i );

	port_w->setLayout( port_w_l );
	layout->addWidget( port_w );


	layout->addSpacing( 10 );


	QLabel* number_l = new QLabel( this );
	number_l->setTextFormat( Qt::RichText );
	number_l->setText( "<b><font size=6>Number of network players</font></b>" );
	layout->addWidget( number_l );


	QWidget* number_w = new QWidget( this );
	QHBoxLayout* number_w_l = new QHBoxLayout( number_w );
	number_w_l->setSpacing( 20 );

	QLabel* plrs_l = new QLabel( number_w );
	plrs_l->setTextFormat( Qt::RichText );
	plrs_l->setText( "<font size=5>Players:</font></b>" );
	number_w_l->addWidget( plrs_l );

	number_i = new QComboBox( number_w );
	ostringstream sout;
	for ( unsigned int i = 2; i <= parameters::get_instance()->game_parameters[ "MAX_PL" ]; ++i )
	{
		sout << i;
		number_i->addItem( sout.str().c_str() );
		sout.str( "" );
	}
	number_w_l->addWidget( number_i );

	number_w->setLayout( number_w_l );
	layout->addWidget( number_w );


	layout->addSpacing( 10 );


	QLabel* load_l = new QLabel( this );
	load_l->setTextFormat( Qt::RichText );
	load_l->setText( "<b><font size=6>Map loading</font></b>" );
	layout->addWidget( load_l );


	QWidget* load_w = new QWidget( this );
	QHBoxLayout* load_w_l = new QHBoxLayout( load_w );

	load_i = new QLineEdit( load_w );
	load_i->setFixedWidth( 250 );
	load_i->setText( "maps/map.map" );
	load_w_l->addWidget( load_i );

	load_filename_b = new QPushButton( "...", load_w );
	load_filename_b->setFixedWidth( 20 );
	load_w_l->addWidget( load_filename_b );
	connect( load_filename_b, SIGNAL( clicked() ), this, SLOT( load_filename_map() ) );

	load_b = new QPushButton( "Load", load_w );
	load_b->setFixedWidth( 150 );
	load_w_l->addWidget( load_b );
	connect( load_b, SIGNAL( clicked() ), this, SLOT( load_map() ) );

	load_w->setLayout( load_w_l );
	layout->addWidget( load_w );


	layout->addSpacing( 10 );


	QLabel* object_l = new QLabel( this );
	object_l->setTextFormat( Qt::RichText );
	object_l->setText( "<b><font size=6>Objects loading</font></b>" );
	layout->addWidget( object_l );


	QWidget* object_w = new QWidget( this );
	QHBoxLayout* object_w_l = new QHBoxLayout( object_w );

	object_i = new QLineEdit( object_w );
	object_i->setFixedWidth( 250 );
	object_i->setText( "objects/objects.obj" );
	object_w_l->addWidget( object_i );

	object_filename_b = new QPushButton( "...", object_w );
	object_filename_b->setFixedWidth( 20 );
	object_w_l->addWidget( object_filename_b );
	connect( object_filename_b, SIGNAL( clicked() ), this, SLOT( load_filename_objects() ) );

	object_b = new QPushButton( "Load", object_w );
	object_b->setFixedWidth( 150 );
	object_b->setEnabled( false );
	object_w_l->addWidget( object_b );
	connect( object_b, SIGNAL( clicked() ), this, SLOT( load_objects() ) );

	object_w->setLayout( object_w_l );
	layout->addWidget( object_w );


	this->setLayout( layout );
	//this->setMaximumSize( 489, 404 );
};



void setup_widget::load_filename_map()
{
	load_i->setText( QFileDialog::getOpenFileName( this, "Load map", "./maps", "Map files (*.map)" ) );
};



void setup_widget::load_filename_objects()
{
	object_i->setText( QFileDialog::getOpenFileName( this, "Load objects", "./objects", "Objects files (*.obj)" ) );
};



void setup_widget::load_map()
{
	if( server_listener::get_instance()->load_map_file( load_i->text().toStdString().c_str() ) )
	{
		load_b->setEnabled( false );
		load_filename_b->setEnabled( false );
		load_i->setReadOnly( true );
		object_b->setEnabled( true );
		load_b->setText( "OK!" );
		listen_b->setEnabled( true );
	}
};



void setup_widget::load_objects()
{
	if( server_listener::get_instance()->load_objects_file( object_i->text().toStdString().c_str() ) )
	{
		object_b->setEnabled( false );
		object_filename_b->setEnabled( false );
		object_i->setReadOnly( true );
		object_b->setText( "OK!" );
	}
};



void setup_widget::start_listen()
{	
	server_listener::get_instance()->create_listener( number_i->currentText().toInt() );
	number_i->setEnabled( false );
	server_gui* sgui = server_gui::get_instance();
	QScrollArea* sa = new QScrollArea( sgui->g );
	sgui->g->listener_w = new listener_widget( sa );
	sa->setWidget( sgui->g->listener_w );
	sa->setWidgetResizable( true );

	if ( server_listener::get_instance()->start_listener( port_i->text().toInt() ) )
	{
		sgui->g->setup_w->deleteLater();
		sgui->g->setup_w = 0;
		sgui->g->setCentralWidget( sa );
		sgui->g->setMinimumSize( 558, 474 );
		sgui->g->resize( 558, 474 );
	}
	else
	{
		sa->deleteLater();
	}
};